package array

func MaxIncreaseKeepingSkyline(grid [][]int) int {

	tMax := []int{}
	lMax := []int{}

	for i := 0; i < len(grid[0]); i++ {
		max := -1
		for j := 0; j < len(grid); j++ {
			if grid[j][i] > max {
				max = grid[j][i]
			}
		}
		tMax = append(tMax, max)
	}

	for i := 0; i < len(grid); i++ {
		max := -1
		for j := 0; j < len(grid[0]); j++ {
			if grid[i][j] > max {
				max = grid[i][j]
			}
		}
		lMax = append(lMax, max)
	}

	ans := 0

	for i := 0; i < len(grid); i++ {
		for j := 0; j < len(grid[0]); j++ {
			max := tMax[i]
			if tMax[i] > lMax[j] {
				max = lMax[j]
			}
			ans += max - grid[i][j]
		}
	}

	return ans

}
